package com.ruoyi.system.service.impl;


import com.ruoyi.lifecycle.domain.LifManageList;
import com.ruoyi.lifecycle.service.ILifManageListService;
import com.ruoyi.system.domain.RelatedTodoevent;
import com.ruoyi.system.domain.SysMarket;
import com.ruoyi.system.domain.SysMarketFile;
import com.ruoyi.system.domain.SysTodoevent;
import com.ruoyi.system.mapper.SysMarketStateMapper;
import com.ruoyi.system.service.IRelatedTodoeventService;
import com.ruoyi.system.service.ISysMarketService;
import com.ruoyi.system.service.ISysMarketStateService;
import com.ruoyi.system.service.ISysTodoeventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class SysMarketStateServiceImpl implements ISysMarketStateService {

    @Autowired
    private SysMarketStateMapper sysMarketStateMapper;

    @Autowired
    private ILifManageListService lifManageListService;
    @Autowired
    private IRelatedTodoeventService relatedTodoeventService;
    @Autowired
    private ISysTodoeventService sysTodoeventService;
    @Autowired
    private ISysMarketService sysMarketService;
    @Override
    public int updateYesState(Long id) {
        Long sysDId = sysMarketStateMapper.selectSysDeviceIdByMarId(id);
        SysMarket sysMarket = sysMarketService.selectMarketById(id);
        String devName = sysMarket.getDevName();
        lifManageListService.updateMarkeYestState(sysDId);
        //更新相关待办事项
        RelatedTodoevent relatedTodoevent = relatedTodoeventService.selectRelatedTodoeventByDevId(sysDId);
        if(null!=relatedTodoevent){
            SysTodoevent sysTodoevent = sysTodoeventService.selectSysTodoeventById(relatedTodoevent.getTodoeventId());
            LifManageList list = lifManageListService.selectLifManageListByDeviceId(relatedTodoevent.getRelatedId());
            sysTodoevent.setReminder("请您评审"+list.getDeviceName()+"市场调研报告");
            sysTodoeventService.updateSysTodoevent(sysTodoevent);
        }
        return sysMarketStateMapper.updateYesState(id);
    }

    @Override
    public int insertSysMarketState(SysMarket sysMarket) {
        Long sysDid = sysMarket.getMarId();
        Long dId = sysMarketStateMapper.selectSysDeviceIdBySysDid(sysDid);
        lifManageListService.updateSysMarketStateByDid(dId);
        //修改待办事项
        RelatedTodoevent relatedTodoevent = relatedTodoeventService.selectRelatedTodoeventByDevId(dId);
        if(null!=relatedTodoevent){
            SysTodoevent sysTodoevent = sysTodoeventService.selectSysTodoeventById(relatedTodoevent.getTodoeventId());
            sysTodoevent.setStatus(1L);
            sysTodoeventService.updateSysTodoeventById(sysTodoevent);
        }
        //删除待办事项中间表
        relatedTodoeventService.deleteRelatedTodoeventById(dId);
        return sysMarketStateMapper.insertSysMarketState(sysMarket);
    }

    @Override
    public Long insertPathName(SysMarketFile sysMarket) {
        Date date = new Date();
        sysMarket.setUpTime(date);
        return sysMarketStateMapper.insertPathName(sysMarket);
    }

    @Override
    public int updateFileState(Long marId) {
        return lifManageListService.updateMarketState(marId);
    }

}
